package com.itheima.exception;


import com.itheima.pojo.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    //指定能够处理的异常类型
    @ExceptionHandler(Exception.class)
    public Result ex1(Exception e){
        //打印堆栈中的异常信息
        e.printStackTrace();

        return Result.error("对不起,操作失败,请联系管理员");
    }
//SQLIntegrityConstraintViolationException
    //实验证明 读不到SQLIntegrityConstraintViolationException这个更精确的错误
    @ExceptionHandler(DuplicateKeyException.class)
    public Result ex1(DuplicateKeyException duplicateKeyException){
        //打印堆栈中的异常信息
        duplicateKeyException.printStackTrace();

        return Result.error("不能输入重复数据,请重试");
}



}
